package com.cll.learn.rabbitmq.controller;

import com.cll.learn.rabbitmq.util.AjaxResult;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * 描述信息:
 * 测试调用rabbitmq
 * @author CLL
 * @version 1.0
 * @date 2021/2/28 10:39
 */
@RestController
@RequestMapping(value = "/rabbitmq/message")
public class RabbitMQController {

    @Autowired
    AmqpTemplate amqpTemplate;

    @RequestMapping(value = "/sendMessage")
    public AjaxResult<String> sendMessage(String message){
        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("UTF-8")
                .setHeader("token", "cll")
                .build();

        Message sendMessage = new Message(message.getBytes(StandardCharsets.UTF_8), messageProperties);
//        amqpTemplate.send(sendMessage);
        amqpTemplate.send("order_ex", "order_ex_q", sendMessage);
        return new AjaxResult<>(200, "操作成功", "消息发送成功");
    }

    @RequestMapping(value = "/sendTtlMessage")
    public AjaxResult<String> sendTtlMessage(String message, String token){
        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("UTF-8")
                .setHeader("token", "cll")
                .build();

        Message sendMessage = new Message(message.getBytes(StandardCharsets.UTF_8), messageProperties);
//        amqpTemplate.send(sendMessage);
        amqpTemplate.send("order_ex", "order_ex_q", sendMessage);
        return new AjaxResult<>(200, "操作成功", "消息发送成功");
    }
}